package nl.gamayun.whohasit;

import nl.gamayun.whohasit.utils.WhoHasItUtils;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class SplashActivity extends Activity implements AnimationListener
{

	static final private String	TAG = SplashActivity.class.getSimpleName();

	private static final Resources resources = WhoHasIt.getContext().getResources();
	private static final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(WhoHasIt.getContext());
	private boolean show;
	protected int	_splashTime	= 1000; // time to display the splash screen in ms

	/** Called when the activity is first created. */
	
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		Log.d(TAG, "onCreate");
		super.onCreate(savedInstanceState);

		String preferences_show_splash_key = resources.getString(R.string.preferences_show_splash_key);
		show = sharedPrefs.getBoolean(preferences_show_splash_key, true);
		if (show)
		{

			setContentView(R.layout.splash);
			
			// Animation animation = AnimationUtils.loadAnimation(this,
			// android.R.anim.slide_in_left);
			TextView version = (TextView) findViewById(R.id.version);
			version.setText(getString(R.string.version, WhoHasItUtils.getAppVersion()));
			Animation animation = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
			animation.setDuration(1000);
			animation.setAnimationListener(this);
			ImageView splashIcon = (ImageView) findViewById(R.id.splash_icon);
			splashIcon.startAnimation(animation);
		}
		else
		{
			showMain();
		}

	}
	
	private void showMain()
	{
		Log.d(TAG, "showMain");
		/* Create an Intent that will start the Main-Activity. */
		Intent mainIntent = new Intent(SplashActivity.this,WhoHasItActivity.class);
		SplashActivity.this.startActivity(mainIntent);
		SplashActivity.this.finish();
	}

	public void onAnimationEnd(Animation arg0)
	{
		Log.d(TAG, "onAnimationEnd");
		new Handler().postDelayed(new Runnable()
		{
			public void run() 
			{
				showMain();
			}
		}, _splashTime);
	}

	public void onAnimationRepeat(Animation animation)
	{
		Log.d(TAG, "onAnimationRepeat");
		// TODO Auto-generated method stub

	}

	public void onAnimationStart(Animation animation)
	{
		Log.d(TAG, "onAnimationStart");
		// TODO Auto-generated method stub

	}

}